//
//  TheColorLabel.m
//  VideoData
//
//  Created by pro1 on 2019/3/8.
//  Copyright © 2019 pro1. All rights reserved.
//

#import "TheColorLabel.h"

@interface TheColorLabel ()

@property (strong,nonatomic) UIView *endView;
@property (strong,nonatomic) UIView *topColor;
@property (strong,nonatomic) UIView *bottomColor;
@property (assign,nonatomic) CGPoint beginPoint;


@end

@implementation TheColorLabel

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.cutOffPoint = frame.origin.y + frame.size.height/2;
        self.backgroundColor = [UIColor clearColor];
        [self addSubview:self.endView];
        self.userInteractionEnabled = NO;
        UIPanGestureRecognizer * pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panHandle:)];
        [self addGestureRecognizer:pan];
        [self sendSubviewToBack:self.endView];
        self.clipsToBounds = YES;
        self.textAlignment = NSTextAlignmentLeft;
        self.font = [UIFont boldSystemFontOfSize:30];
        
        [self layoutIfNeeded];
        [self reloadView];
        self.endView.frame = CGRectMake(0, self.frame.size.height, 1, self.frame.size.height);
    }
    return self;
}

- (void)setTopColorValue:(UIColor *)topColorValue{
    _topColorValue = topColorValue;
    if (_topColorValue) {
        _topColor.backgroundColor = _topColorValue;
    }
}

- (void)setBottomColorValue:(UIColor *)bottomColorValue{
    _bottomColorValue = bottomColorValue;
    if (_bottomColorValue) {
        _bottomColor.backgroundColor = _bottomColorValue;
    }
}

- (void)reloadView{
    CGFloat topHeight = self.cutOffPoint - self.frame.origin.y;
#pragma mark - <动态计算两个色块的高度>
    if (topHeight > 0 && topHeight < self.frame.size.height) {
        [self.topColor mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.offset(topHeight);
        }];
        [self.bottomColor mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.offset(self.frame.size.height - topHeight);
        }];
    }
    else if(topHeight < 0){
        [self.topColor mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.offset(0);
        }];
        [self.bottomColor mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.offset(self.frame.size.height);
        }];
    }
    else{
        [self.topColor mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.offset(self.frame.size.height);
        }];
        [self.bottomColor mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.offset(0);
        }];
    }
    self.textColor = [UIColor colorWithPatternImage:[self convertViewToImage:self.endView]];
}

- (UIView *)endView{
    if (!_endView) {
        _endView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, self.frame.size.height)];
        [_endView addSubview:self.topColor];
        [_endView addSubview:self.bottomColor];
        [_topColor mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.right.equalTo(self.endView).offset(0);
            make.height.offset(self.frame.size.height/2);
        }];
        [_bottomColor mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.left.right.equalTo(self.endView).offset(0);
            make.height.offset(self.frame.size.height/2);
        }];
    }
    return _endView;
}

- (UIView *)topColor{
    if (!_topColor) {
        _topColor  = [[UIView alloc] init];
        _topColor.backgroundColor = [UIColor whiteColor];
    }
    return _topColor;
}

- (UIView *)bottomColor{
    if (!_bottomColor) {
        _bottomColor = [[UIView alloc] init];
        _bottomColor.backgroundColor = [UIColor blackColor];
    }
    return _bottomColor;
}

-(void)panHandle:(UIPanGestureRecognizer *)pan{
    if (pan.state == UIGestureRecognizerStateEnded) {
        
    }
    else if (pan.state == UIGestureRecognizerStateBegan){
        _beginPoint = [pan translationInView:self];
    }
    else if (pan.state == UIGestureRecognizerStateChanged){
        CGFloat length = [pan translationInView:self].y - _beginPoint.y;
        CGRect frame = self.frame;
        frame.origin.y += length;
        self.frame = frame;
        _beginPoint = [pan translationInView:self];
        [self reloadView];
    }
}

#pragma mark 生成image
//使用该方法不会模糊，根据屏幕密度计算
- (UIImage *)convertViewToImage:(UIView *)view {
    UIImage *imageRet = [[UIImage alloc]init];
    //UIGraphicsBeginImageContextWithOptions(区域大小, 是否是非透明的, 屏幕密度);
    UIGraphicsBeginImageContextWithOptions(view.frame.size, YES, [UIScreen mainScreen].scale);
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    imageRet = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return imageRet;
}

@end
